package order.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import common.model.order.OrderItem;

@Mapper
public interface OrderItemMapper extends BaseMapper<OrderItem> {
    
    /**
     * 查询用户是否购买过特定SPU商品
     * @param userId 用户ID
     * @param spuId 商品SPU ID
     * @return 订单号，如果没有购买过则返回null
     */
    @Select("SELECT oi.order_no FROM order_item oi " +
           "JOIN orders o ON oi.order_no = o.order_no " +
           "WHERE o.user_id = #{userId} AND oi.spu_id = #{spuId} " +
           "AND o.status >= 1 " + // 已支付或更高状态的订单
           "LIMIT 1")
    String findOrderNoByUserIdAndSpuId(Long userId, Long spuId);
}
